package com.hfh.net;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author hfh
 * @Date 2022 09 13 19 33
 **/
public class Test52 {
    @Test
    public void client() throws IOException {
        Socket s = new Socket(InetAddress.getLocalHost(),9999);

        PrintWriter pw = new PrintWriter(s.getOutputStream());

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

        Scanner scan = new Scanner(System.in);
        while (true){
            String str = scan.next();
            pw.println(str);
            pw.flush();

            String resp =br.readLine();
            System.out.println(resp);
            if ("exit".equals(str)){
                break;
            }
        }

        br.close();
        pw.close();
        s.close();
    }
    @Test
    public void server() throws IOException {
        ServerSocket ss = new ServerSocket(9999);
        Socket s = ss.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter pw = new PrintWriter(s.getOutputStream());

        while (true){
            String str = br.readLine();
            if ("exit".equals(str)){
                pw.println("bye");
                pw.flush();
                break;
            }else {
                pw.print(str.toUpperCase());
                pw.flush();
            }
        }
        pw.close();
        br.close();
        s.close();
        ss.close();
    }
}
